package com.kiln.juxingqs.fragment.market

import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.lifecycle.MutableLiveData
import client.global.commlib.base.BaseFragment
import client.global.commlib.utils.gson
import client.global.commlib.widget.EmptyLayout
import com.github.mikephil.charting.listener.LoadMoreListener
import com.github.mikephil.charting.stockChart.dataManage.KLineDataManage
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.chart.KLineEntity
import com.kiln.juxingqs.databinding.FragmentKlinemoremutBinding
import com.kiln.juxingqs.ui.vm.KLineModel
import com.kiln.juxingqs.utils.DateUtils
import com.kiln.juxingqs.utils.chart.DataHelper
import com.kiln.juxingqs.utils.chart.DataRequest
import org.json.JSONArray

/**
 * 日K
 */
class ChartMinuteMoreKLineFragment : BaseFragment<FragmentKlinemoremutBinding, KLineModel>() {


    private var goodsId: String? = null

    override fun getViewId(): Int = R.layout.fragment_klinemoremut

    override fun initViewModel(): KLineModel = createViewModel()

    override fun initWidget() {
        super.initWidget()
        goodsId = requireArguments().getString("goodsId")
        binding.combinedchart.setLoadmore(object : LoadMoreListener() {
            override fun onLoadMore() {
                this@ChartMinuteMoreKLineFragment.onLoadMore()
            }

            override fun fling(dX: Float, dY: Float) {

            }

        })
        binding.combinedchart.initChart(requireArguments().getBoolean("landscape"))
        binding.emptyLayout.setOnClickListener {
            onLoadMore()
        }
    }


    val kDate = MutableLiveData<String>("")

    override fun initData() {
        super.initData()
        viewModel.callkLine1m.observe(this, {
            it?.let {
                if (it.isSuccess) {
//                    fillData(it.data?.day_k_list as MutableList<KLineEntity?>)


                    val kLineData = KLineDataManage(requireContext())
                    DataHelper.calculateMA(it.data?.data as List<KLineEntity>)
                    val `object` = JSONArray(gson.toJson(it.data?.data))
                    //上证指数代码000001.IDX.SH
                    //上证指数代码000001.IDX.SH
                    kLineData.parseMutKlineMoreData(`object`, "000001.IDX.SH", false, true)
//                    kDate.value = it.data!!.k_date

//                    if (kDate.value.equals("None")) {
                    binding.combinedchart.setNoMore(true)
//                    }
                    viewModel.callkLine.value = null
                    binding.combinedchart.setDataToChartAndShuaxin(kLineData)

                    binding.emptyLayout.visibility = View.GONE
                } else {
                    if (binding.combinedchart.getkLineData() == null) {
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                    }
                }
                binding.combinedchart.setLoading(false)
                handler.postDelayed(runnable, 5000) //每5秒执行一次runnable. -第一步
            }
        })

    }

    val handler = Handler()

    val runnable: Runnable = object : Runnable {
        override fun run() {
//            //要做的事情
            if (DateUtils.isBelong(viewModel.begin_time, viewModel.end_time)) {
                onLoadMore()
            } else {
//                LogUtils.i("非开盘时间范围内")
                handler.postDelayed(this, 5000) //每5秒执行一次runnable. -第一步
            }
        }
    }

    companion object {
        fun newInstance(type: Int, land: Boolean, goodsId: String): ChartMinuteMoreKLineFragment {
            val fragment = ChartMinuteMoreKLineFragment()
            val bundle = Bundle()
            bundle.putInt("type", type)
            bundle.putBoolean("landscape", land)
            bundle.putString("goodsId", goodsId)
            fragment.arguments = bundle
            return fragment
        }
    }

    fun onLoadMore() {
        viewModel.holeKLinemut(goodsId!!, 5, kDate.value)

    }

    override fun onResume() {
        super.onResume()
        onLoadMore()
    }

    override fun onPause() {
        super.onPause()
        handler.removeCallbacks(runnable)
        handler.removeCallbacksAndMessages(null)
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacks(runnable)
        handler.removeCallbacksAndMessages(null)
    }

}